package com.mcx.modules.proto;

import com.mcx.modules.common.IProto;
import com.mcx.modules.common.MMO_DataStream;

import java.io.*;
import java.util.ArrayList;

public class RoleOperation_LogOnGameServerReturnProto implements IProto {
    private static MMO_DataStream dataStream = new MMO_DataStream();
    @Override
    public short getProtoCode() {
        return 10002;
    }

    public int RoleCount; //已有角色数量
    public ArrayList<RoleItem> RoleList; //角色项

    /// <summary>
    /// 角色项
    /// </summary>
    public static class RoleItem
    {
        public int RoleId; //角色编号
        public String RoleNickName; //角色昵称
        public byte RoleJob; //角色职业
        public int RoleLevel; //角色等级
    }

    public byte[] ToArray() {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try (DataOutputStream outputStream = new DataOutputStream(out)) {
            dataStream.outputStream = outputStream;
            dataStream.WriteShort(getProtoCode());
            dataStream.WriteInt(RoleCount);
            for (int i = 0; i < RoleCount; i++)
            {
                dataStream.WriteInt(RoleList.get(i).RoleId);
                dataStream.WriteUTF8String(RoleList.get(i).RoleNickName);
                dataStream.outputStream.writeByte(RoleList.get(i).RoleJob);
                dataStream.WriteInt(RoleList.get(i).RoleLevel);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return out.toByteArray();
    }

    public static RoleOperation_LogOnGameServerReturnProto GetProto(byte[] buffer)
    {
        RoleOperation_LogOnGameServerReturnProto proto = new RoleOperation_LogOnGameServerReturnProto();
        try (DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(buffer))) {
            dataStream.inputStream = inputStream;
            proto.RoleCount = dataStream.ReadInt();
            proto.RoleList = new ArrayList<RoleItem>();
            for (int i = 0; i < proto.RoleCount; i++)
            {
                RoleItem _Role = new RoleItem();
                _Role.RoleId = dataStream.ReadInt();
                _Role.RoleNickName = dataStream.ReadUTF8String();
                _Role.RoleJob = dataStream.inputStream.readByte();
                _Role.RoleLevel = dataStream.ReadInt();
                proto.RoleList.add(_Role);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return proto;
    }
}
